package com.iengoo.spacewar;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief 爆炸对象
 * @email 122162005@qq.com
 * @date 2018-12-27 00:59
 */
public class Bomb {
    private int x;
    private int y;
    private static BufferedImage blastImage; // 爆炸整体图片对象
    private BlastCoordinate[] bcs;  // 单个爆炸效果图片

    static {
        try {
            blastImage = ImageIO.read(Bomb.class.getResource("resources/explosion_.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private List<BlastCoordinate> currentBCs = new ArrayList<>();  // 当前爆炸显示图片
    private static final int SIZE = 32;
    private static final int survivalTimeSeed = 1; // 存活时间种子
    private long createTime;

    public Bomb(int x, int y) throws IOException {
        // 初始化当前爆炸显示图片
        this.x = x;
        this.y = y;

        bcs = new BlastCoordinate[]{
                new BlastCoordinate(0, 0, 32, 32, x, y),
                new BlastCoordinate(32, 0, 64, 32, x, y),
                new BlastCoordinate(64, 0, 96, 32, x, y)
        };

        // 产生一个3到6之间的爆炸特效数组
        int bcNum = 3 + (int) (Math.random() * 3);
        for (int i = 0; i < bcNum; i++) {
            currentBCs.add(bcs[(int) (Math.random() * bcs.length)]);
        }

        createTime = System.currentTimeMillis();
    }

    /**
     * 绘制当前爆炸的图片
     *
     * @param g
     */
    public void paint(Graphics g) {
        for (int i = 0; i < currentBCs.size(); i++) {
            BlastCoordinate bc = currentBCs.get(i);
            g.drawImage(blastImage,
                    (int)(bc.x += bc.xSpeed),
                    (int)(bc.y += bc.ySpeed),
                    (int)bc.x + SIZE, (int)bc.y + SIZE,
                    bc.sx1, bc.sy1, bc.sx2, bc.sy2,
                    null);
        }
    }

    /**
     * 爆炸特效在图片中的坐标
     */
    class BlastCoordinate {
        private int sx1;  // 贴图起点x坐标
        private int sy1;  // 贴图起点y坐标
        private int sx2;  // 贴图终点x坐标
        private int sy2;  // 贴图终点y坐标
        private double x;  // 爆炸效果出现的初始化x位置
        private double y;  // 爆炸效果出现的初始化y位置
        private double xSpeed;
        private double ySpeed;
        private double liveTime; // 存活时间秒
        private double[] plusMinus = new double[] {-1, 1};

        public BlastCoordinate(int sx1, int sy1, int sx2, int sy2, int x, int y) {
            this.sx1 = sx1;
            this.sy1 = sy1;
            this.sx2 = sx2;
            this.sy2 = sy2;
            this.x = x;
            this.y = y;
            Random random = new Random();
            this.xSpeed = (((random.nextInt(3) + 2) / 10.0d)) * plusMinus[random.nextInt(2)];
            this.ySpeed = (((random.nextInt(3) + 2) / 10.0d)) * plusMinus[random.nextInt(2)];
            // this.liveTime = (int) (Math.random() * survivalTimeSeed + 0.4);
            this.liveTime = (random.nextInt(3) + 2) / 10.0;
        }

        public double getLiveTime() {
            return liveTime;
        }
    }

    public List<BlastCoordinate> getCurrentBCs() {
        synchronized (currentBCs) {
            return currentBCs;
        }
    }

    public long getCreateTime() {
        return createTime;
    }
}
